const app = getApp();
const API_URL = app.globalData.API_URL;

Page({
  data: {
    customBar: app.globalData.CustomBar,
    ROOT_URL: app.globalData.ROOT_URL,
    TabCur: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let state = options.state;
    this.setData({
      TabCur: state,
    })
    let openid = wx.getStorageSync('openid');
    this.getOrders(openid, state);
  },

  tabSelect(e) {
    let state = e.currentTarget.dataset.id;
    this.setData({
      TabCur: state,
    })
    let openid = wx.getStorageSync('openid');
    this.getOrders(openid, state);
  },

  getOrders(openid, state) {
    let that = this;
    wx.request({
      url: API_URL + "getOrders",
      method: 'POST',
      data: {
        'openid': openid,
        'state': state
      },
      success(res) {
        that.setData({
          orders: res.data.data,
        })
      }
    })
  },

  repayOrder(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    wx.request({
      url: API_URL + "repayOrder",
      method: 'POST',
      data: {
        'id': id
      },
      success(res) {
        console.log(res);
        let data = res.data;
        data['success'] = () => {
          console.log('微信支付成功');
          let openid = wx.getStorageSync('openid');
          that.getOrders(openid, 1);
        };
        data['fail'] = () => {
          console.log('微信支付失败');
        };
        wx.requestPayment(data);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})